#pragma once

#include <iostream>
#include "TcpServer.hpp"
#include"HttpProtocol.hpp"
namespace HttpServerMoudule
{
    using namespace TcpServerMoudule;
    class HttpServer
    {
    public:
        HttpServer(int port)
            :_tcpsr(std::make_unique<TcpServer>(port))
        {}
        void Start()
        {
            _tcpsr->InitServer([this](SockPtr sockfd, InetAddr *clientaddr){ return this->HttpHandleEntry(sockfd,clientaddr);});
            _tcpsr->Loop();
        }
        //处理http请求的入口
        bool HttpHandleEntry(SockPtr sockfd, InetAddr *clientaddr)
        {
            LOG(LogLevel::DEBUG) << "HttpServer: get a new client: " << sockfd->Fd() << " addr info: " << clientaddr->GetAddr();
            std::string http_request;
            sockfd->Recv(&http_request);
            std::cout<<http_request<<std::endl;
            HttpRequest req;
            //将请求反序列化
            req.Deserialize(http_request);
            HttpResponse resp;
            resp.build(req);
            std::string resp_str;
            resp.Seserialize(&resp_str);
            sockfd->Send(resp_str);
            return true;
        }
        ~HttpServer()
        {
        }

    private:
        std::unique_ptr<TcpServer> _tcpsr;
    };
}